
<template>
    <theme-box title="版本添加">
        <form class="form-horizontal" @submit.prevent v-el:form>
            <div class="box-body">
                <form-input title="版本号" :required="true" name="versionNo"></form-input>
                <form-select title="版本类别" :options="typeOptions" :required="true" name="versionType"></form-select>
                <form-select title="是否强制更新" :required="true" :options="forceOptions" name="forced"></form-select>
                <form-input title="更新链接" :required="true" name="linkUrl"></form-input>
            </div>
            <!-- /.box-body -->
            <div class="box-footer">
                <v-button type="reset" @click="reset">重置</v-button>
                <v-button type="submit" theme="primary" class="pull-right">确认</v-button>
            </div>
            <!-- /.box-footer -->
        </form>
    </theme-box>

</template>

<script>
    import 'plugins/jquery-validation';
    import {dateTime} from 'ui/collections/general.js'

    var serialize = require('form-serialize');

    export default {
        data: () => ({
            usrName:"",
            oprId:"",
            typeOptions:[
                {key:"android",value:"android"},
                {key:"ios",value:"ios"},
            ],
            forceOptions:[
                {key:'是',value:'y'},
                {key:'否',value:'n'}
            ],
        }),
        methods:{
            reset() {
                $("label.error").remove();
                this.$els.form.reset()
            }
        },
        ready() {
            dateTime()
            var self = this;
            $(this.$els.form).validate({
                submitHandler: async function() {
                    var _data = serialize(this.currentForm, { hash: true, empty:true  });
                    var oprName = JSON.parse(localStorage.getItem("id_token")).usrName;
                    var oprId = JSON.parse(localStorage.getItem("id_token")).oprId;
                    _data.oprName = oprName;
                    _data.oprId = oprId;
                    await self.$post({
                        url: "/api/v1"+ projectName_Url+ "/plat/release/postRelease",
                        data: _data
                    })
                    let data = {
                        title: "操作成功",
                        content: "是否进入版本列表查看?"
                    }
                    Midway.confirm(data).then(function () {
                        self.$route.router.go('/version/list');
                    })
                    self.reset()
                },
                rules:{
                    versionNo :{
                        required:true,
                        version:true,
                        maxlength:16
                    },
                    versionType :{
                        required: true
                    },
                    linkUrl:{
                        required: true,
                        url:true,
                        maxlength:64
                    },
                    forced:{
                        required: true
                    },
                    oprId:{
                        required: true,
                        int:true,
                        min:0
                    },
                    oprName:{
                        required: true
                    }
                },
                messages:{

                }
            });
        }
    }
</script>
